/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillExt;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class BusinessBillListExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"BusinessBillList.xls\"");
		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setAlignment(2);
		titleCellStyle.setVerticalAlignment(1);
		HSSFCellStyle boldStyle = workbook.createCellStyle();
		boldStyle.setFont(boldFont);
		boldStyle.setBorderBottom(1);
		boldStyle.setBorderTop(1);
		boldStyle.setBorderLeft(1);
		boldStyle.setBorderRight(1);
		HSSFCellStyle norStyle = workbook.createCellStyle();
		norStyle.setBorderBottom(1);
		norStyle.setBorderTop(1);
		norStyle.setBorderLeft(1);
		norStyle.setBorderRight(1);
		HSSFSheet sheet = workbook.createSheet("list");
		List list = (List) model.get("list");
		BigDecimal costTotal = (BigDecimal) model.get("costTotal");
		BigDecimal adjustTotal = (BigDecimal) model.get("adjustTotal");
		BigDecimal collectTotal = (BigDecimal) model.get("collectTotal");
		BigDecimal remainTotal = costTotal.add(adjustTotal).subtract(
				collectTotal);
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		byte colNum = 21;

		int b;
		for (b = 1; b < colNum; ++b) {
			r.createCell((short) b);
		}

		c.setCellStyle(titleCellStyle);
		c.setCellValue(new HSSFRichTextString("业务单一览表"));
		sheet.addMergedRegion(new Region(0, 0, 0, (short) (colNum - 1)));
		r = sheet.createRow(1);
		c = r.createCell(0);

		for (b = 1; b < colNum; ++b) {
			r.createCell((short) b);
		}

		c.setCellValue(new HSSFRichTextString("金额合计：" + costTotal + "（已优惠："
				+ model.get("favTotal") + "元） 调账：" + adjustTotal + "元 已收："
				+ collectTotal + "元（其中预付款：" + model.get("imprestTotal")
				+ "元） 欠款：" + remainTotal + "元 面积合计：" + model.get("squareTotal")
				+ "平方米 长度合计：" + model.get("lengMTotal") + "米 "
				+ model.get("lengCmTotal") + "公分 合同单数：" + list.size() + "笔"));
		sheet.addMergedRegion(new Region(1, 0, 1, (short) (colNum - 1)));
		r = sheet.createRow(2);
		c = r.createCell(0);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("业务单号"));
		c = r.createCell(1);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("订货日期"));
		c = r.createCell(2);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("订货单位"));
		c = r.createCell(3);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("摘要"));
		c = r.createCell(4);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("交货日期"));
		c = r.createCell(5);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("完工日期"));
		c = r.createCell(6);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("结清日期"));
		c = r.createCell(7);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("金额"));
		c = r.createCell(8);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("调账"));
		c = r.createCell(9);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("已收"));
		c = r.createCell(10);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("欠款"));
		c = r.createCell(11);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("业务员"));
		c = r.createCell(12);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("设计员"));
		c = r.createCell(13);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("跟单员"));
		c = r.createCell(14);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("营销来源"));
		c = r.createCell(15);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("开单人"));
		c = r.createCell(16);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("最后打印"));
		c = r.createCell(17);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("审核状态"));
		c = r.createCell(18);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("完工状态"));
		c = r.createCell(19);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("送货状态"));
		c = r.createCell(20);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("结清状态"));
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			BusinessBill arg33 = (BusinessBill) objs[0];
			BusinessBillExt be = (BusinessBillExt) objs[1];
			String estName = (String) objs[2];
			String deliDate = "";
			String compDate = "";
			String closDate = "";
			if (arg33.getDeliverDate() != null) {
				deliDate = sf.format(arg33.getDeliverDate());
			}

			if (be.getCompleteTime() != null) {
				compDate = sf.format(be.getCompleteTime());
			}

			if (be.getCloseTime() != null) {
				closDate = sf.format(be.getCloseTime());
			}

			if (arg33.getLinkman() != null && !arg33.getLinkman().equals("")) {
				estName = estName + "(" + arg33.getLinkman() + ")";
			}

			String printStr = "";
			String exaStr = "";
			String compStr = "";
			String deliStr = "";
			String closStr = "";
			if (be.getPrintNum() > 0) {
				printStr = be.getLastPrinter() + "/" + be.getPrintNum();
			}

			if (be.getExamined().equals("Y")) {
				exaStr = "已审核";
			} else {
				exaStr = "未审核";
			}

			if (be.getCompleted().equals("Y")) {
				compStr = "已完工";
			} else {
				compStr = "未完工";
			}

			if (be.getDelivered().equals("Y")) {
				deliStr = "已送完";
			} else if (be.getDelivered().equals("N")) {
				deliStr = "未送货";
			} else if (be.getDelivered().equals("P")) {
				deliStr = "未送完";
			}

			if (be.getClosed().equals("Y")) {
				closStr = "已结清";
			} else {
				closStr = "未结清";
			}

			BigDecimal remain = arg33.getCost().add(be.getAdjustCost())
					.subtract(be.getCollectedCost());
			r = sheet.createRow(os + 3);
			c = r.createCell(0);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(arg33.getBillCode()));
			c = r.createCell(1);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(sf.format(arg33
					.getOrderDate())));
			c = r.createCell(2);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(estName));
			c = r.createCell(3);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(arg33.getContent()));
			c = r.createCell(4);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(deliDate));
			c = r.createCell(5);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(compDate));
			c = r.createCell(6);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(closDate));
			c = r.createCell(7);
			c.setCellStyle(norStyle);
			c.setCellValue(arg33.getCost().doubleValue());
			c = r.createCell(8);
			c.setCellStyle(norStyle);
			c.setCellValue(be.getAdjustCost().doubleValue());
			c = r.createCell(9);
			c.setCellStyle(norStyle);
			c.setCellValue(be.getCollectedCost().doubleValue());
			c = r.createCell(10);
			c.setCellStyle(norStyle);
			c.setCellValue(remain.doubleValue());
			c = r.createCell(11);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(arg33.getManagerName()));
			c = r.createCell(12);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(arg33.getDesignerNameStr()));
			c = r.createCell(13);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(arg33.getFollowerNameStr()));
			c = r.createCell(14);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(arg33.getIntroducerName()));
			c = r.createCell(15);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(arg33.getCreaterName()));
			c = r.createCell(16);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(printStr));
			c = r.createCell(17);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(exaStr));
			c = r.createCell(18);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(compStr));
			c = r.createCell(19);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(deliStr));
			c = r.createCell(20);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(closStr));
		}

		ServletOutputStream arg34 = response.getOutputStream();
		workbook.write(arg34);
		arg34.flush();
		arg34.close();
	}
}